package com.mindskip.xzs.repository;

import com.mindskip.xzs.domain.Message;
import com.mindskip.xzs.repository.base.MyMapper;
import com.mindskip.xzs.viewmodel.admin.message.MessagePageRequestVM;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MessageMapper extends MyMapper<Message> {
//    int deleteByPrimaryKey(Integer id);
//
//    int insert(Message record);
//
//    int insertSelective(Message record);
//
//    Message selectByPrimaryKey(Integer id);
//
//    int updateByPrimaryKeySelective(Message record);
//
//    int updateByPrimaryKey(Message record);

    String queryParams = " id, title, content, create_time createTime, send_user_id sendUserId, send_user_name sendUserName, " +
            "send_real_name sendRealName, receive_user_count receiveUserCount, read_count readCount ";

    @Select("<script>" +
            "select " + queryParams +
            "from t_message" +
            "<if test=\"sendUserName != null\">\n" +
            "where  send_user_name  like concat('%',#{sendUserName},'%')\n" +
            "</if>" +
            "</script>")
    List<Message> page(MessagePageRequestVM requestVM);

    @Select("<script>" +
            "select" + queryParams +
            "from t_message " +
            "where  id  in " +
            "<foreach item=\"id\" collection=\"list\" open=\"(\" separator=\",\"  close=\")\">\n" +
            "#{id}\n" +
            "</foreach>" +
            "</script>")
    List<Message> selectByIds(List<Integer> ids);

    @Update("UPDATE t_message SET read_count = read_count + 1\n" +
            "      WHERE id= #{id} and read_count = (SELECT m.read_count from ( SELECT read_count FROM t_message WHERE id = #{id} ) m)")
    int readAdd(@Param("id") Integer id);
}
